﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NhaCungCap.DTO;
using System.Data.OleDb;
using System.Text;
using System.Data;

namespace NhaCungCap.DAO
{
    public class UserDAO
    {
        public static List<UserDTO> Select()
        {
            List<UserDTO> listUT = new List<UserDTO>();
            DataProvider provider = new DataProvider();
            try
            {
                DataTable tb = provider.ExecuteQuery("SELECT US.idUser, ID_UserType, US.userName, UT.tenLoaiUser, phanQuyen, password, fullName, soDienThoai, email FROM User US, LoaiUser UT WHERE ID_UserType = UT.idLoaiuser");

                foreach (DataRow dr in tb.Rows)
                {
                    UserDTO user = new UserDTO();

                    user.IdUser = Convert.ToInt32(dr[0]);
                    user.UserName = dr[2].ToString();
                    LoaiUserDTO LoaiUser = new LoaiUserDTO();
                    LoaiUser.IdLoaiUser = Convert.ToInt32(dr[1]);
                    LoaiUser.TenLoaiUser = dr[3].ToString();
                    LoaiUser.PhanQuyen = dr[4].ToString();
                    user.LoaiUser = LoaiUser;
                    user.Password = dr[5].ToString();
                    //user.FullName = dr["FullName"].ToString();
                    user.Email = dr["Email"].ToString();
                    user.SoDienThoai = dr["SoDienThoai"].ToString();
                   //user.Block = Convert.ToInt32(dr["Block"]);
                    listUT.Add(user);
                }
            }
            catch (Exception ex)
            {
                listUT = new List<UserDTO>();
            }
            finally
            {
                provider.Disconnect();
            }
            return listUT;
        }

        public static void GetRoleOfUser(string UserName, ref string UserID, ref string Role)
        {
            List<UserDTO> ListUser = Select();
            foreach (UserDTO user in ListUser)
            {
                if (UserName == user.UserName)
                {
                    UserID = user.IdUser.ToString();
                    Role = user.LoaiUser.TenLoaiUser;
                }
            }
        }

        public static bool ValidateUser(string UserName, string Password)
        {
            List<UserDTO> ListUser = Select();
            foreach (UserDTO user in ListUser)
            {
                if (UserName == user.UserName && Password == user.Password)
                {
                    return true;
                }
            }

            return false;
        }

        public static bool InsertUserSimple(UserDTO user)
        {
            DataProvider provider = new DataProvider();
            bool kq = true;
            try
            {
                //String query = "INSERT INTO User VALUES( NULL, " + user.TenDayDu + "," + user.DiaChi + "," + user.Email + "," + user.SoDienThoai + ", " + user.UserName + ", " + user.Password + "," + user.SoTaiKhoan + "," + user.TrangThaiUser + ")";

                String query = "INSERT INTO Members( Email, UserName, Password) VALUES('" + user.Email + "', '" + user.UserName + "', '" + user.Password + "')";
                kq = provider.ExecuteNonQuery(query);
            }

            catch (Exception ex)
            {
                kq = false;
            }
            finally
            {
                provider.Disconnect();
            }
            return kq;
        }
    }
}